package com.zz.ecommerce.declareManage.generateDeclare;

import com.zz.ecommerce.common.bean.qdc.db.TDeclformHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TGoodsListQdc;
import com.zz.ecommerce.common.bean.qdc.db.TOrderHeadQdc;
import com.zz.ecommerce.common.bean.qdc.db.TWaybillHeadQdc;
import com.zz.ecommerce.common.constants.TableConstants;
import com.zz.ecommerce.declareManage.common.dao.QdcGoodsListDao;
import com.zz.ecommerce.declareManage.common.dao.TDeclformHeadQdcDao;
import com.zz.ecommerce.utils.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

/**
 * Created by admin on 2016/3/30.
 */
@Service
public class GenerateDeclareService {


    @Autowired
    private TDeclformHeadQdcDao tDeclformHeadQdcDao;

    @Autowired
    private QdcGoodsListDao qdcGoodsListDao;

    @Autowired
    private GenerateDeclareDao generateDeclareDao;


    @Transactional(readOnly = false)
    public boolean generateDeclare(TOrderHeadQdc orderHead, TWaybillHeadQdc wayHead, List<TGoodsListQdc> goodsList, HttpSession session) {

        TDeclformHeadQdc tDeclformHeadQdc = new TDeclformHeadQdc();

        String declId = UUID.randomUUID().toString();
        tDeclformHeadQdc.setId(declId);

        tDeclformHeadQdc.setIeFlag(orderHead.getIeFlag());
        tDeclformHeadQdc.setTradeMode(orderHead.getTradeMode());
        tDeclformHeadQdc.setTradeCode(orderHead.getEbCode());
        tDeclformHeadQdc.setAgentCode(orderHead.getEbCode());
        tDeclformHeadQdc.setEbCode(orderHead.getEbCode());
        tDeclformHeadQdc.setTradeName(orderHead.getEbName());
        tDeclformHeadQdc.setAgentName(orderHead.getEbName());
        tDeclformHeadQdc.setEbName(orderHead.getEbName());
        tDeclformHeadQdc.setTransTotalPrice(orderHead.getTotalPayment());
        tDeclformHeadQdc.setOwnerName(orderHead.getBuyerName());
        tDeclformHeadQdc.setOwnerCertType(orderHead.getBuyerCertType());
        tDeclformHeadQdc.setOwnerCertId(orderHead.getBuyerCertId());
        tDeclformHeadQdc.setTradeCount(orderHead.getBuyerCountry());
        tDeclformHeadQdc.setOwnerTel(orderHead.getBuyerTel());
        tDeclformHeadQdc.setOwnerAddr(orderHead.getDeliveryAddr());
        tDeclformHeadQdc.setEbPlatId(orderHead.getEbPlatId());
        tDeclformHeadQdc.setEbPlatName(orderHead.getEbPlatId());//todo 此处应根据平台代码获取平台名称
        tDeclformHeadQdc.setNote(orderHead.getNote());

        tDeclformHeadQdc.setWaybillId(wayHead.getWaybillId());
        tDeclformHeadQdc.setTrafMode(wayHead.getTrafMode());
        tDeclformHeadQdc.setDeclPort(wayHead.getDeclPort());
        tDeclformHeadQdc.setIePort(wayHead.getIePort());
        tDeclformHeadQdc.setSuperPlaceCode(String.valueOf(wayHead.getIePort()));
        tDeclformHeadQdc.setSuperPlaceName(String.valueOf(wayHead.getIePort()));
        tDeclformHeadQdc.setTrafName(wayHead.getTrafName());
        tDeclformHeadQdc.setVoyageNo(wayHead.getVoyageNo());
        tDeclformHeadQdc.setBillNo(wayHead.getBillNo());
        tDeclformHeadQdc.setPackId(wayHead.getPackId());
        tDeclformHeadQdc.setLogiEnteCode(wayHead.getLogiEnteCode());
        tDeclformHeadQdc.setPaycopCode(wayHead.getLogiEnteCode());
        tDeclformHeadQdc.setExpressCopCode(wayHead.getLogiEnteCode());
        tDeclformHeadQdc.setLogiEnteName(wayHead.getLogiEnteName());
        tDeclformHeadQdc.setPaycopName(wayHead.getLogiEnteName());
        tDeclformHeadQdc.setExpressCopName(wayHead.getLogiEnteName());
        tDeclformHeadQdc.setCurrCode(wayHead.getCurrCode());
        tDeclformHeadQdc.setGrossWeight(wayHead.getGrossWeight());
        tDeclformHeadQdc.setPackNum(wayHead.getPackNum());

        tDeclformHeadQdc.setReturnFlag("0");
        tDeclformHeadQdc.setRelSeqNo("");
        tDeclformHeadQdc.setRelWaybillId("");
        tDeclformHeadQdc.setRelLogiEntecode("");
        tDeclformHeadQdc.setIeDate("");//todo 此处进境日期/出境日期没有
        tDeclformHeadQdc.setManualNo("");//todo 此处账册编号没有

        //设置为B2C
        tDeclformHeadQdc.setBusinessType("0");

        //设置申报标志为E1
        tDeclformHeadQdc.setStatus("E1");

        //设置APP_STEP_ID为新建
        tDeclformHeadQdc.setAppStepId("A");

        //设置操作人代码和组织机构代码
        tDeclformHeadQdc.setOperId(SessionUtil.getOperator(session));
        tDeclformHeadQdc.setOrgId(SessionUtil.getOrganizationId(session));

        //清单表头入库
        tDeclformHeadQdcDao.insert(TableConstants.T_DECLFORM_HEAD_QDC, tDeclformHeadQdc);

        TGoodsListQdc tGoodsListQdc = new TGoodsListQdc();
        for (int j = 0; j < goodsList.size(); j++) {
            tGoodsListQdc.setId(UUID.randomUUID().toString());
            tGoodsListQdc.setRelatedId(declId);

            tGoodsListQdc.setgNo(goodsList.get(j).getgNo());
            tGoodsListQdc.setgName(goodsList.get(j).getgName());
            tGoodsListQdc.setCodeTs(goodsList.get(j).getCodeTs());
            tGoodsListQdc.setgDesc(goodsList.get(j).getgDesc());
            tGoodsListQdc.setgModel(goodsList.get(j).getgModel());
            tGoodsListQdc.setgNum(goodsList.get(j).getgNum());
            tGoodsListQdc.setgUnit(goodsList.get(j).getgUnit());
            tGoodsListQdc.setPrice(goodsList.get(j).getPrice());
            tGoodsListQdc.setTotalPrice(goodsList.get(j).getPrice().multiply(goodsList.get(j).getgNum()));
            tGoodsListQdc.setCurrCode(goodsList.get(j).getCurrCode());
            tGoodsListQdc.setNote(goodsList.get(j).getNote());

//            tGoodsListQdc.setQty1(0);     // TODO: 2016/3/31 没有原始数据    法定数量
//            tGoodsListQdc.setUnit1("");     // TODO: 2016/3/31 没有原始数据    法定单位
//            tGoodsListQdc.setTradeMark("");     // TODO: 2016/3/31 没有原始数据    品牌
//            tGoodsListQdc.setOriginCountry("");     // TODO: 2016/3/31 没有原始数据    原产国/目的国
//            tGoodsListQdc.setUseTo("");     // TODO: 2016/3/31 没有原始数据    用途
//            tGoodsListQdc.setDutyMode("");     // TODO: 2016/3/31 没有原始数据    征免方式
//            tGoodsListQdc.setTaxPayment("");     // TODO: 2016/3/31 没有原始数据    税款
//            tGoodsListQdc.setTaxRate("");     // TODO: 2016/3/31 没有原始数据    税率


            //清单表体入库
            qdcGoodsListDao.insert(TableConstants.T_GOODS_LIST_QDC, tGoodsListQdc);
        }

        generateDeclareDao.updateIFCreate(TableConstants.T_ORDER_HEAD_QDC, orderHead.getId());
        generateDeclareDao.updateIFCreate(TableConstants.T_WAYBILL_HEAD_QDC, wayHead.getId());
        return true;
    }
}
